<?php
namespace app\web\controller;

use app\model\Theme; 

class Qrlist extends Base
{
    public function Qrlist()
    {
global $_G, $_P;
$system = $_G['system'];
$_G['page']['title'] = '二维码管理 - ' . $system['sysname'];
//load_func('tpl');
$GLOBALS['frames'] = array();
$themeid = intval($_P['themeid']);
if($themeid <= 0){
	$themeid = Theme::getDefaultThemenew();
}
$theme = Theme::getThemeById($themeid);
if(empty($theme['id'])){
	return_msg('还没有创建活动,请联系管理员');
}
$config = 	Theme::getConfig($themeid);
$do = 'qrlist';
$id = intval($_P['id']);
$op = !empty($_P['op']) ? $_P['op'] : 'display';
$url = createWebUrl($do,array('op' => 'display','themeid'=>$themeid));

if($op == 'display') {
	$pageNumber = max ( 1, intval ( $_P['page'] ) );
	$pageSize = 50;
	$sql = "SELECT * FROM " . tablename ( t_qrlist ) . " 
				WHERE themeid = '{$themeid}' ORDER BY state desc,displayorder desc, id desc  LIMIT " . ($pageNumber - 1) * $pageSize . ',' . $pageSize;
	$list = db_select ( $sql);

	$sql =  "SELECT COUNT(id) FROM " . tablename ( t_qrlist ) . " 
				WHERE themeid = '{$themeid}' ORDER BY state desc,displayorder desc, id desc";
	$total = db_fetchcolumn ($sql,$params);
	
	$pager = pagination ( $total, $pageNumber, $pageSize );
}
if($op == 'addbank'){
	if (!empty($id)) {
		$sql = "SELECT * FROM " . tablename ( t_qrlist ) . " 
				WHERE id = '{$id}' ";
		$qrlist = db_find ( $sql);
	}else{
		$qrlist['qrnum'] = 1;
		$qrlist['state'] = 1;
	}
	if(checksubmit('submit')) {
		$data = array(
            'uniacid'  => $_G['mpid'],
            'themeid'  => $themeid,
			'qrnum' => intval($_P['qrnum']),
			'displayorder' => intval($_P['displayorder']),
			'state' => intval($_P['state']),
			'addtime' => TIMESTAMP,
		);
		if (empty($id)) {
			$qrimg = $_P['qrimg'];
			foreach ($qrimg as $index => $item) {
				$data['qrimg'] = tomedia($item);
				$insertid = db_insert(t_qrlist, $data);
			}
		}else{
			$data['usercount'] = intval($_P['usercount']);
			$data['qrimg'] = tomedia($_P['qrimg']);
			db_update(t_qrlist, $data, array('id' =>$id));			
		}
        return_tips('操作成功',$url, 'success');
	}
}
if($op == 'status') {
	$status = intval($_P['status']);
	$state = db_update(t_qrlist, array('state' => $status), array('id' => $id));
	return_tips(error(0, ''), '', 'ajax');
}

$return_clicent = get_defined_vars();
return view('/qrlist' , $return_clicent);
}
}